package org.buptsse.page;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;
import org.buptsse.entity.HistoryItem;
import org.buptsse.entity.User;
import org.buptsse.entity.UserInfoItem;

import org.buptsse.ui.MessageAllActivity;
import org.buptsse.ui.R;
import org.buptsse.ui.TestActivity;
import org.buptsse.utils.APPConstant;
import org.buptsse.utils.ConnectionChecker;
import org.buptsse.utils.GloableInstance;
import org.buptsse.utils.HttpHelper;
import org.buptsse.utils.JsonUtils;

import com.google.gson.reflect.TypeToken;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;

public class MyInfoPage extends LinearLayout implements OnClickListener {
	private final String SAVE_PATH = "/mnt/sdcard/tmp/";
	private View viewpersonal;
	private User user = null;
	private Button quwen, guanzhu, fansi,inforSet,circleCreate;
	private Context context;
	private ImageView headImage = null;
	
	private TextView usernameTextView,degreeTextView,coinTextView,emailTextView,
						telTextView,schoolTextView,secondSubjectTextView;
	
	
	
	public MyInfoPage(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public MyInfoPage(Context context, User user) {
		super(context);
		this.context = context;
		this.user = null;
		
		init();
	}

	private void init() {
		LayoutInflater inflater = LayoutInflater.from(getContext());
		viewpersonal = inflater.inflate(R.layout.lmy_setting_info, null);
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		addView(viewpersonal, params);
		
		usernameTextView=(TextView) viewpersonal.findViewById(R.id.username);
		degreeTextView=(TextView) viewpersonal.findViewById(R.id.degree);
		coinTextView=(TextView) viewpersonal.findViewById(R.id.coin);
		emailTextView=(TextView) viewpersonal.findViewById(R.id.email);
		telTextView=(TextView) viewpersonal.findViewById(R.id.phone_number);
		schoolTextView=(TextView) viewpersonal.findViewById(R.id.school);
		secondSubjectTextView=(TextView) viewpersonal.findViewById(R.id.second_subject);
	}

	//刷新用户信息
	public void updateUserData()
	{
		
	}
	
	@Override
	public void onClick(View v) {
		Intent it = new Intent();
		switch (v.getId()) {
		
		}
	}
	
	public void UpdateUserData(Context context,Handler handler)
	{
		GetUserDataThread gt=new GetUserDataThread(context, handler);
		gt.start();
	}
	
	public void FillUserInfo()
	{
		usernameTextView.setText(GloableInstance.user.getName());
		degreeTextView.setText(GloableInstance.user.getDegree());
		coinTextView.setText(GloableInstance.user.getCoin());
		emailTextView.setText(GloableInstance.user.getMail());
		telTextView.setText(GloableInstance.user.getPhoneNumber());
		schoolTextView.setText(GloableInstance.user.getSchool());
		secondSubjectTextView.setText(GloableInstance.user.getSecondSubject());
	}
	
	private class GetUserDataThread extends Thread
	{
		private Context context;
		private Handler handler;
		
		public GetUserDataThread(Context context,Handler handler)
		{
			this.context=context;
			this.handler=handler;
		}
		
		public void run()
		{
			if(ConnectionChecker.CheckConnection(context, handler)==false)
			{
				return;
			}
			ArrayList<NameValuePair> param=new ArrayList<NameValuePair>();
			param.add(new BasicNameValuePair("username", GloableInstance.user.getName()));
			param.add(new BasicNameValuePair("password", GloableInstance.user.getPassword()));
			try {
				String json=HttpHelper.sendPost(APPConstant.getUserDataUrl, param);
				if(!json.equals("failure"))
				{
					List<UserInfoItem> list=(LinkedList<UserInfoItem>)JsonUtils.getJsonObject(json, new TypeToken<LinkedList<UserInfoItem>>() {}.getType());
					Message msg=handler.obtainMessage(APPConstant.getUserDataFinish);
					UserInfoItem item=list.get(0);
					msg.obj=list.get(0);
					
					GloableInstance.user.setDegree(item.getDegree());
					GloableInstance.user.setSecondSubject(item.getSecondsubject());
					GloableInstance.user.setPhoneNumber(item.getPhone());
					GloableInstance.user.setSchool(item.getSchool());
					GloableInstance.user.setMail(item.getMail());
					GloableInstance.user.setCoin(item.getCoin());
					GloableInstance.user.setPhoto(item.getPhoto());
					handler.sendMessage(msg);
					return;
				}else {
					Message msg=handler.obtainMessage(APPConstant.getUserDataError);
					handler.sendMessage(msg);
					return;
				}
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
